#include "mainwindow.h"
#include "QPushButton"
#include "QComboBox"
#include "QCheckBox"
void MainWindow::init_ui(){
    //设置背景图片以及棋盘初始化
    QPixmap pix(":/imgs\\cb.png");
    setFixedSize(pix.width(),pix.height());
    QPalette palette;
    palette.setBrush(QPalette::Window,QBrush(pix));
    this->setPalette(palette);
    //设置重启按钮
    QPushButton * restart = new QPushButton(this);
    restart->setText("重新开始");
    restart->move(25,pix.height()-90);
    //设置模式按钮
    QPushButton * randombtn = new QPushButton(this);
    randombtn->setText("随机走子");
    randombtn->move(25,20);
    QPushButton * humanbtn = new QPushButton(this);
    humanbtn->setText("人人对战");
    humanbtn->move(25,50);
    QPushButton * computerbtn = new QPushButton(this);
    computerbtn->setText("人机对战");
    computerbtn->move(25,80);
    QPushButton * c_vs_cbtn = new QPushButton(this);
    c_vs_cbtn->setText("机机对战");
    c_vs_cbtn->move(25,110);
    QPushButton *undobtn = new QPushButton(this);
    undobtn->setText("悔棋");
    undobtn->move(width() - ydis-undobtn->width(),height() - 50);  
    QPushButton *backbtn = new QPushButton(this);
    QPushButton *gobtn = new QPushButton(this);
    backbtn->setText("回退一步");
    backbtn->move(width() - ydis - undobtn->width() - 30 -backbtn->width(),height() - 50);
    gobtn->setText("前进一步");
    gobtn->move(width() - ydis - undobtn->width() - 150 -backbtn->width(),height() - 50);
    setnow->setText("中断搜索");
    setnow->move(width() - ydis - undobtn->width() - 270 -backbtn->width(),height() - 50);
    QCheckBox * save_check = new QCheckBox(this);
    save_check->setText("自动保存棋局");
    save_check->setCheckState(Qt::Checked);
    save_check->move(width() - ydis - 100,height() - 100);

    btn_set.insert(backbtn);
    btn_set.insert(gobtn);
    btn_set.insert(randombtn);
    btn_set.insert(computerbtn);
    btn_set.insert(undobtn);
    btn_set.insert(restart);
    btn_set.insert(humanbtn);
    btn_set.insert(c_vs_cbtn);
    setnow->setDisabled(true);
    box = new QComboBox(this);
    box->addItem("随机先手");
    box->addItem("玩家先手");
    box->addItem("电脑先手");
    box->move(25,pix.height()-130);

    QFont font;
    font.setFamily("楷体");
    font.setBold(true);
    font.setItalic(true);
    font.setPointSize(30);
    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::blue);//设置颜色
    wait->setText("Waiting...");
    wait->setFont(font);
    wait->setPalette(pe);
    wait->resize(200,200);
    wait->move(xdis+150,ydis + 150);
    wait->hide();

    connect(box,&QComboBox::currentIndexChanged,[=]{
        select = box->currentText();
        resetcb();
        if(select == "电脑先手" && mode == computer_vs_human) next();
    });
    connect(randombtn,&QPushButton::clicked,[=]{
        mode = random;
        resetcb();
        isfin = false;
        next();
    });
    connect(humanbtn,&QPushButton::clicked,[=]{
        mode = human;
        isfin = false;
        resetcb();
        next();
    });
    connect(c_vs_cbtn,&QPushButton::clicked,[=]{
        mode = c_vs_c;
        resetcb();
        isfin = false;
        next();
    });
    connect(computerbtn,&QPushButton::clicked,[=]{
        mode = computer_vs_human;
        resetcb();
        isfin = false;
        cal_t = new class std::thread([this](){
            engine->reset_hash(isfirst);
         });
         cal_t->detach();
        next();
    });
    connect(restart,&QPushButton::clicked,this,&MainWindow::resetcb);
    connect(this,&MainWindow::complete,this,&MainWindow::next);
    connect(undobtn,&QPushButton::clicked,this,&MainWindow::undo);
    connect(backbtn,&QPushButton::clicked,this,&MainWindow::back);
    connect(gobtn,&QPushButton::clicked,this,&MainWindow::go);
    connect(setnow,&QPushButton::clicked,[this](){
        emit stop();
    });
    connect(save_check,&QCheckBox::stateChanged,[this,save_check](){
        if(save_check->isChecked()) save = true;
        else save = false;
    });
    initCb();

    engine = new Engine(this);
    engine->setParent(this);
    connect(engine,&Engine::notyify_ui,this,&MainWindow::engine_notyify);
    lastStep->resize(300,30);
    lastStep->setStyleSheet("border:2px solid black");
    lastStep->move(width() - ydis - 500,height() - 130);
    fh->move(25,this->height()-50);

    QFont timefont;
    timefont.setPointSize(15);
    black_timer->setParent(this);
    white_timer->setParent(this);
    black_timer->resize(130,50);
    white_timer->resize(130,50);
    black_timer->move(15,160);
    white_timer->move(15,220);
    black_timer->setStyleSheet("border:5px solid black");
    white_timer->setStyleSheet("border:5px solid black");
    initTimer();
    black_timer->setFont(timefont);
    white_timer->setFont(timefont);
    black_timer->show();
    white_timer->show();
    QPixmap icon(":/imgs\\icon.jpg");
    setWindowIcon(QIcon(icon));
    setIconSize(icon.size());
}

void MainWindow::initCb(){
    cb_with_num.chess_num = 8;
    isfirst = false;
    current.time = 0;
    for (int i = 0; i <= 143;i++) {
        cb_with_num.chessborad[i] = 2;
    }
    for (int i = 0; i <= 143;i+=12) {
        cb_with_num.chessborad[i] = 3;
    }
    for (int i = 0; i <= 11; i++) {
        cb_with_num.chessborad[i] = 3;
    }
    for (int i = 11;i <= 143; i+=12) {
        cb_with_num.chessborad[i] = 3;
    }
    for (int i = 132;i <= 143;i++) {
        cb_with_num.chessborad[i] = 3;
    }
    cb_save.clear();
    cur_cb = -1;
    initTimer();
    firsthand();
    cb_with_num.chessborad[16] = 0;cb_with_num.movechess.whitechess[0]= 16;
    cb_with_num.chessborad[19] = 0; cb_with_num.movechess.whitechess[1]= 19;
    cb_with_num.chessborad[49] = 0; cb_with_num.movechess.whitechess[2]= 49;
    cb_with_num.chessborad[58] = 0; cb_with_num.movechess.whitechess[3]= 58;
    cb_with_num.chessborad[85] = 1; cb_with_num.movechess.blackchess[0]= 85;
    cb_with_num.chessborad[94] = 1; cb_with_num.movechess.blackchess[1]= 94;
    cb_with_num.chessborad[124] = 1;cb_with_num.movechess.blackchess[2]= 124;
    cb_with_num.chessborad[127] = 1; cb_with_num.movechess.blackchess[3]= 127;
    cb_with_num.blackcurrenthinder = 0;
    cb_with_num.blackcurrentmove = 0;
    cb_with_num.blackcurrentset = 0;
    cb_with_num.whitecurrenthinder = 0;
    cb_with_num.whitecurrentmove = 0;
    cb_with_num.whitecurrentset = 0;
    cb_save.push_back(cb_with_num);
    cur_cb++;
}

void MainWindow::resetcb(){
    initCb();
    update();
}

void MainWindow::initTimer(){
    black_timer->reset();
    white_timer->reset();
}
